package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kang.bean.Dict;
import com.kang.mapper.DictMapper;
import com.kang.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @program: service
 * @description: 字典服务类
 * @author: K.faWu
 * @create: 2022-04-26 13:40
 **/
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Override
    public PageInfo<Dict> query(Dict dict, Pageable page) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(dict)){
            if (dict.getType()!=null){
                wrapper.like("type", dict.getType());
            }else{
                wrapper.eq("type", "type");
            }
            if (dict.getCode()!=null){
                wrapper.like("code", dict.getCode());
            }
            if (dict.getText()!= null){
                wrapper.like("text", dict.getText());
            }
            if (dict.getSpare()!= null){
                wrapper.like("spare", dict.getSpare());
            }
        }else{
            wrapper.eq("type", "type");
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<Dict> list = dictMapper.selectList(wrapper);
        return new PageInfo<>(list);
    }

    @Override
    public void add(Dict dict) {
        dictMapper.insert(dict);
    }
}
